package com.zxl.yjb.activity.my;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zxl.yjb.R;
import com.zxl.yjb.app.Const;
import com.zxl.yjb.base.BaseActivity;
import com.zxl.yjb.bean.IncomeListBean;
import com.zxl.yjb.bean.LoginSucessBean;
import com.zxl.yjb.fragment.my.MyWalletDetailFragment;
import com.zxl.yjb.http.manager.DataManager;
import com.zxl.yjb.http.observer.ObserverOnNextListener;
import com.zxl.yjb.http.observer.ProgressObserver;
import com.zxl.yjb.utils.SaveBeanUtil;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyWalletActivity extends BaseActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_page_title)
    TextView tvPageTitle;
    @BindView(R.id.tv_account_balance)
    TextView tvAccountBalance;
    @BindView(R.id.tv_withdrawals)
    TextView tvWithdrawals;
    @BindView(R.id.ll_withdrawals)
    LinearLayout llWithdrawals;
    @BindView(R.id.tv_income_details)
    TextView tvIncomeDetails;
    @BindView(R.id.rl_income_details)
    RelativeLayout rlIncomeDetails;
    @BindView(R.id.tv_present_record)
    TextView tvPresentRecord;
    @BindView(R.id.rl_present_record)
    RelativeLayout rlPresentRecord;
    @BindView(R.id.frame_layout)
    FrameLayout frameLayout;
    @BindView(R.id.v_income_details)
    View vIncomeDetails;
    @BindView(R.id.v_present_record)
    View vPresentRecord;
    private LoginSucessBean loginSucessBean;
    private MyWalletDetailFragment incomeFragment;
    private MyWalletDetailFragment withdrawFragment;
    private FragmentManager supportFragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myallet);
        ButterKnife.bind(this);
        Object bean = SaveBeanUtil.getBean(this, Const.VIPINFO);
        if (bean != null) {
            loginSucessBean = (LoginSucessBean) bean;
        }
        initView();
        initFragment();
        getIncomeList();
    }

    private void initFragment() {
        if (incomeFragment == null){
            incomeFragment = new MyWalletDetailFragment();
            Bundle bundle = new Bundle();
            bundle.putString("type","income");
            incomeFragment.setArguments(bundle);
        }
        if (withdrawFragment == null){
            withdrawFragment = new MyWalletDetailFragment();
            Bundle bundle = new Bundle();
            bundle.putString("type","withdraw");
            withdrawFragment.setArguments(bundle);
        }
        supportFragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        if (!incomeFragment.isAdded())
            fragmentTransaction.add(R.id.frame_layout, incomeFragment);
        if (!withdrawFragment.isAdded())
            fragmentTransaction.add(R.id.frame_layout, withdrawFragment);
        fragmentTransaction.hide(withdrawFragment).show(incomeFragment).commit();
    }


    @Override
    protected void initView() {
        tvPageTitle.setText(R.string.my_wallet);
        /*tvAccountBalance.setText(loginSucessBean.getUser_money());
        tvWithdrawals.setText(loginSucessBean.getUser_money());*/
    }


    private void getIncomeList()
    {
        DataManager dataManager = new DataManager();
        ObserverOnNextListener<IncomeListBean> listener = new ObserverOnNextListener<IncomeListBean>() {
            @Override
            public void onNext(IncomeListBean incomeListBean) {
                tvAccountBalance.setText(incomeListBean.getAccount_balance());
                tvWithdrawals.setText(incomeListBean.getMay_tx());
            }
        };
        dataManager.incomeList(new ProgressObserver<IncomeListBean>(listener,this),new HashMap<String, String>());
    }


    @OnClick({R.id.iv_back, R.id.ll_withdrawals, R.id.rl_income_details, R.id.rl_present_record})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.ll_withdrawals:
                startActivity(new Intent(this, WithdrawalsAcitivity.class));
                break;
            case R.id.rl_income_details:
                tvIncomeDetails.setTextColor(getResources().getColor(R.color.common_yellow_color));
                tvPresentRecord.setTextColor(getResources().getColor(R.color.common_text_color));
                vIncomeDetails.setVisibility(View.VISIBLE);
                vPresentRecord.setVisibility(View.INVISIBLE);
                supportFragmentManager.beginTransaction().hide(withdrawFragment).show(incomeFragment).commit();
                break;
            case R.id.rl_present_record:
                tvPresentRecord.setTextColor(getResources().getColor(R.color.common_yellow_color));
                tvIncomeDetails.setTextColor(getResources().getColor(R.color.common_text_color));
                vPresentRecord.setVisibility(View.VISIBLE);
                vIncomeDetails.setVisibility(View.INVISIBLE);
                supportFragmentManager.beginTransaction().hide(incomeFragment).show(withdrawFragment).commit();
                break;
        }
    }


    public void refreshData()
    {
        getIncomeList();
    }
}
